﻿namespace HiMap.Geometry
{
    using System;
    using System.Collections.Generic;

    public class PolylineClass : GeometryClass, IPolyline, IPointCollection, IGeometry, IGeometryCollection
    {
        private List<IGeometry> geoList;
        private List<IPoint> ptList;

        public PolylineClass()
        {
            base._Type = GeometryType.Polyline;
            this.geoList = new List<IGeometry>();
            this.ptList = new List<IPoint>();
            this.geoList.Add(this);
            base._Envelope = new EnvelopeClass();
        }

        void IGeometryCollection.AddGeometry(IGeometry Geo)
        {
            this.geoList.Add(Geo);
        }

        IGeometry IGeometryCollection.GetGeometry(int Index)
        {
            return this.geoList.get_Item(Index);
        }

        void IGeometryCollection.InsertGeometry(int Index, IGeometry Geo)
        {
            this.geoList.Insert(Index, Geo);
        }

        void IGeometryCollection.RemoveGeometry(int Index, int Count)
        {
            this.geoList.RemoveRange(Index, Count);
        }

        void IGeometryCollection.SetGeometry(int Index, IGeometry NewGeo)
        {
            this.geoList.set_Item(Index, NewGeo);
        }

        void IPointCollection.AddPoint(IPoint Geo)
        {
            this.ptList.Add(Geo);
        }

        IPoint IPointCollection.GetPoint(int Index)
        {
            return this.ptList.get_Item(Index);
        }

        void IPointCollection.InsertPoint(int Index, IPoint Geo)
        {
            this.ptList.Insert(Index, Geo);
        }

        void IPointCollection.RemovePoint(int Index, int Count)
        {
            this.ptList.RemoveRange(Index, Count);
        }

        void IPointCollection.SetPoint(int Index, IPoint NewGeo)
        {
            this.ptList.set_Item(Index, NewGeo);
        }

        IEnvelope IGeometry.Envlope
        {
            get
            {
                IEnvelope envelope = new EnvelopeClass();
                for (int i = 0; i < this.geoList.get_Count(); i++)
                {
                    IPointCollection points = (IPointCollection) this.geoList.get_Item(i);
                    for (int j = 0; j < points.PointCount; j++)
                    {
                        IPoint point = points.GetPoint(j);
                        if ((i == 0) && (j == 0))
                        {
                            envelope.MinX = point.X;
                            envelope.MaxX = point.X;
                            envelope.MinY = point.Y;
                            envelope.MaxY = point.Y;
                        }
                        else
                        {
                            envelope.MinX = Math.Min(point.X, envelope.MinX);
                            envelope.MaxX = Math.Max(point.X, envelope.MaxX);
                            envelope.MinY = Math.Min(point.Y, envelope.MinY);
                            envelope.MaxY = Math.Max(point.Y, envelope.MaxY);
                        }
                    }
                }
                return envelope;
            }
        }

        int IGeometryCollection.GeometryCount
        {
            get
            {
                return this.geoList.get_Count();
            }
        }

        int IPointCollection.PointCount
        {
            get
            {
                return this.ptList.get_Count();
            }
        }
    }
}

